//
//  WSCategoryViewController.m
//  GroupFourFM
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 GroupFour. All rights reserved.
//

#import "WSCategoryViewController.h"
#import "WSAppTools.h"
#import "WSModel.h"
#import "WHFooterCollectionReusableView.h"
#import "WHHeaderCollectionReusableView.h"
#import "WHCollectionViewCell.h"
#import "WHCategoryScrollController.h"

@interface WSCategoryViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,WHHeaderCollectionReusableViewDelegate>

@property (nonatomic, retain) UICollectionView *myCollectionView;
@property (nonatomic, retain) NSMutableArray *myArray;//用来装分区的数组
@property (nonatomic, retain) NSMutableArray *classListArray;//用来存储所有数据
@property (nonatomic, retain) NSMutableArray *firstArray;//放5个btn的数据

@end

@implementation WSCategoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor yellowColor];
    // Do any additional setup after loading the view.
    self.title = @"分类听";
    
    [self createCollectionView];
    [self getWorking];
    /**
     *  数组初始化
     */
    self.myArray = [NSMutableArray array];
    self.classListArray = [NSMutableArray array];
    self.firstArray = [NSMutableArray array];
    
    // 导航栏下面的一条黑线
    UIView *lineUp = [[UIView alloc] init];
    lineUp.backgroundColor = [UIColor colorWithRed:0.87 green:0.87 blue:0.87 alpha:1];
    [self.view addSubview:lineUp];
    [lineUp mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.left.mas_equalTo(@0);
        make.top.mas_equalTo(@0);
        make.height.mas_equalTo(@1);
    }];

    
}
#pragma mark - 创建UICollectionView

-(void)createCollectionView{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WID*375/2 - 20, 40);
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    flowLayout.footerReferenceSize = CGSizeMake(WID*375, 20);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:flowLayout];
    [self.view addSubview:self.myCollectionView];
    
    [self.myCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view);
    }];
    
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    self.myCollectionView.showsVerticalScrollIndicator = NO;
    [self.myCollectionView registerClass:[WHCollectionViewCell class] forCellWithReuseIdentifier:@"WHCollectionViewCell"];
    //注册表头区域
    [self.myCollectionView registerClass:[WHHeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [self.myCollectionView registerClass:[WHFooterCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
    
    
}
#pragma mark - 请求网络数据

-(void)getWorking{
    
    [WSAppTools getDataWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v1/categories?device=iPhone&picVersion=11&scale=2" block:^(id result) {
        
        NSDictionary *dic = result;
        NSMutableArray *array = [dic objectForKey:@"list"];
        for (NSMutableDictionary *listDic in array) {
            
            WSModel *list = [WSModel modelWithDictionary:listDic];
            //[list setValuesForKeysWithDictionary:listDic];
            
            [self.classListArray addObject:list];
            
        }
        
        self.firstArray = [NSMutableArray array];
        for (int i = 0; i < 5; i++) {
            
            WSModel *firstList = [self.classListArray objectAtIndex:i];
            [self.firstArray addObject:firstList];
            
        }

        NSMutableArray *secondArray = [NSMutableArray array];
        for (int i = 5; i < 23; i++) {
            
            WSModel *secondList = [self.classListArray objectAtIndex:i];
            [secondArray addObject:secondList];
            
        }
        NSMutableArray *oneArray = [NSMutableArray array];
        for (int i = 5; i < 11; i++) {
            
            WSModel *oneList = [self.classListArray objectAtIndex:i];
            [oneArray addObject:oneList];
            
        }
        NSMutableArray *twoArray = [NSMutableArray array];
        for (int i = 11; i < 17; i++) {
            
            WSModel *twoList = [self.classListArray objectAtIndex:i];
            [twoArray addObject:twoList];
            
        }
        NSMutableArray *threeArray = [NSMutableArray array];
        for (int i = 17; i < 23; i++) {
            
            WSModel *threeList = [self.classListArray objectAtIndex:i];
            [threeArray addObject:threeList];
            
        }
        NSMutableArray *fourArray = [NSMutableArray array];
        for (int i = 23; i < self.classListArray.count; i++) {
            
            WSModel *fourList = [self.classListArray objectAtIndex:i];
            [fourArray addObject:fourList];
            
        }
        
        self.myArray = [NSMutableArray arrayWithObjects: oneArray, twoArray, threeArray, fourArray, nil];
        
        [self.myCollectionView reloadData];
    } error:^(NSError *error) {
        
        
    }];
    
    
}
#pragma mark - UICollectionView 协议代理方法

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    
    //创建头部区域View
    if (kind == UICollectionElementKindSectionHeader) {
        WHHeaderCollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        headView.backgroundColor = COLOR_BG;
        headView.buttonDelegate = self;
        return headView;
    }else{
        WHFooterCollectionReusableView *footView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
        
        return footView;
    }
    
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    if (section == 0){
        
        return CGSizeMake(WID*375, 195*HEI);
        
    }else{
        
        return CGSizeMake(0, 0);
    }
    
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return self.myArray.count;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSMutableArray *array = [self.myArray objectAtIndex:section];
    
    return array.count;
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    static NSString *indexF = @"WHCollectionViewCell";
    
    WHCollectionViewCell *cellF = [collectionView dequeueReusableCellWithReuseIdentifier:indexF forIndexPath:indexPath];
    
    NSMutableArray *secArray = [self.myArray objectAtIndex:indexPath.section];
    WSModel *listF = [secArray objectAtIndex:indexPath.row];
    cellF.classList = listF;
    cellF.backgroundColor = [UIColor whiteColor];
    return cellF;
    
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    WHCategoryScrollController *WHScrollC = [[WHCategoryScrollController alloc] init];
    
    NSMutableArray *secArray = [self.myArray objectAtIndex:indexPath.section];
    WSModel *listF = [secArray objectAtIndex:indexPath.row];
    //传ID到更多界面
    WHScrollC.moreTitleId = listF.myCategoryId;
    WHScrollC.moreTitleS = listF.title;
    [self.navigationController pushViewController:WHScrollC animated:YES];
    
    
}
-(void)pushToMusic:(NSInteger)buttonTag{
    
    WHCategoryScrollController *WHScrollC = [[WHCategoryScrollController alloc] init];
    WSModel *listF = [self.firstArray objectAtIndex:buttonTag-10001];
    //传ID到更多界面
    WHScrollC.moreTitleId = listF.myCategoryId;
    WHScrollC.moreTitleS = listF.title;
    [self.navigationController pushViewController:WHScrollC animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
